PHP File Upload - image works, pdf does not

PHP File Upload - image works, pdf does not

am 03.01.2008 22:07:13 von ire.kevin

Hello all, first I'd like to say I've always found google groups to be
very helpful, especially considering the help around here is out of
just plane goodness.

The problem I ran into, or rather a friend I had work on this, is
uploading a PDF. We had it working perfectly fine uploading images,
and displaying them. But then someone had the idea of also being able
to upload a PDF. And for whatever reason, it doesn't like to upload
PDF's, and from what I've gathered it isn't giving any errors either.

Maybe some of you guru's could take a look at the 2 files I have shown
below, maybe something will pop out as "huh???".

As an example of how it works... http://www.mlab-ymf.org/careers/jobs/jobs.php

Unfortunately I can't give you the password for the login, it simply
allows users to access the 2nd page, form.php where the file uploader
is.

Thanks for any time or thought given to this.

------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
FILENAME: jobs.php
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------


ob_start();
include '../../header.php';
$fname = 'jobs.php';

?>

$reset = false;
$pw = 'password';



function cookie($name, $data) { return setcookie($name, $data, time()
+ 60 * 60 * 24 * 30, '/'); }

function cookieGet($name, $default = false) {
if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
else
return $default;
}

function login($hash) {
$_SESSION['jLogin'] = $hash;
cookie('jLogin', $hash);

// echo 'Setcookie: ' . (cookie('jLogin', $hash) ? 'yes' : 'no');
// echo 'Hash: ' . $_SESSION['jLogin'];
}

function logout() { login(''); }

function loggedIn() {
global $pw;

$loginHash = cookieGet('jLogin');

if (isset($loginHash))
return $loginHash == md5($pw);

if (isset($_SESSION['jLogin']))
return $_SESSION['jLogin'] == md5($pw);

return false;
}

// Create the jobs file if it does not exist.
if (!file_exists('jobs.txt') || $reset) {
$file = new YFile('jobs');
$array = array(
'jobs' => array()
);
$file->open(true);
$file->set($array);
$file->close();
}

function get($name) {
if (isset($_GET[$name]))
if (get_magic_quotes_gpc())
return htmlspecialchars(stripslashes($_GET[$name]));
else
return htmlspecialchars($_GET[$name]);

if ($name == 'image') return $_POST['image'];
if ($name == 'pdf') return $_POST['pdf'];

if (isset($_POST[$name]))
if (get_magic_quotes_gpc())
return htmlspecialchars(stripslashes($_POST[$name]));
else
return htmlspecialchars($_POST[$name]);
return '';
}

function searchJobs() {
$s = get('search');
$found = false;
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
for($i = 0; $i < sizeof($jobs); $i++)
foreach ($jobs[$i] as $key=>$value)
if (!(strpos(strtolower($value), $s) === false)) {
// echo $key . ' = ' . $value;
echoJob($jobs[$i], $i);
$found = true;
break;
}

if (!$found)
echo 'No results.';


}

function editJob($e) {
$companyname = get('companyname');
$title = get('title');
$salary = get('salary');
$description = get('description');
$requirements = get('requirements');
$extra1 = get('extra1');
$name = get('name');
$pnumber = get('pnumber');
$email = get('email');
$site = get('site');
$address = get('address');

$file = new YFile('jobs');
$file->open(true);
$jobs = $file->get('jobs');

$job = array(
'companyname' => $companyname,
'title' => $title,
'salary' => $salary,
'description' => $description,
'requirements' => $requirements,
'extra1' => $extra1,
'name' => $name,
'pnumber' => $pnumber,
'email' => $email,
'site' => $site,
'address' => $address,
'imagename' => $jobs[$e]['imagename'],
'pdfname' => $jobs[$e]['pdfname']
);

$jobs[$e] = $job;
$file->set($jobs, 'jobs');
$file->close();
}

function deleteJob($e) {
$file = new YFile('jobs');
$file->open(true);
$jobs = $file->get('jobs');
unset($jobs[$e]);
$jobs = array_values($jobs);
$file->set($jobs, 'jobs');
$file->close();
}

function addJob() {
// echo 'test.
';
// print_r($_GET);
// print_r($_POST);
// print_r($_FILES);
if (isset($_FILES['image'])) {
// echo '!!!!!!!!!!!!!!!';
// echo "Name: ".$_FILES['image']['name']."";
// echo "Size: ".$_FILES['image']['size']."";
// echo "Type: ".$_FILES['image']['type']."";
copy ($_FILES['image']['tmp_name'], "jobs.images/" .
$_FILES['image']['name']);


}

if (isset($_FILES['pdf'])) {
// echo '!!!!!!!!!!!!!!!';
// echo "Name: ".$_FILES['image']['name']."";
// echo "Size: ".$_FILES['image']['size']."";
// echo "Type: ".$_FILES['image']['type']."";
copy ($_FILES['pdf']['tmp_name'], "jobs.pfds/" .
$_FILES['pdf']['name']);


}


$companyname = get('companyname');
$title = get('title');
$salary = get('salary');
$description = get('description');
$requirements = get('requirements');
$extra1 = get('extra1');

$name = get('name');
$pnumber = get('pnumber');
$email = get('email');
$site = get('site');
$address = get('address');

$file = new YFile('jobs');
$file->open(true);
$job = array(
'pdfname' => (isset($_FILES['pdf']) ? $_FILES['pdf']['name'] : ''),
'imagename' => (isset($_FILES['image']) ? $_FILES['image']
['name'] : ''),
'companyname' => $companyname,
'title' => $title,
'salary' => $salary,
'description' => $description,
'requirements' => $requirements,
'extra1' => $extra1,
'name' => $name,
'pnumber' => $pnumber,
'email' => $email,
'site' => $site,
'address' => $address,
'timestamp' => time()
);

$jobs = $file->get('jobs');
$jobs[] = $job;
$jobs = array_values($jobs);
$file->set($jobs, 'jobs');
$file->close();
}

function echoJobs() {
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
for($i = sizeof($jobs) - 1; $i >= 0; $i--) {
echo '

';
echo '

' .
$jobs[$i]['title']. ' at ' . $jobs[$i]['companyname'] . '
';
if (($i > -1 ) && loggedIn()) {
echo ' (Edit) a> | ';
echo '
(Delete)';

}
echo '

';



if ($jobs[$i]['imagename']) echo '' .
' >' . '
';
if ($jobs[$i]['pdfname']) echo '' . ' href="jobs.pdfs/' . $jobs[$i]['pdfname'] . '" />Link to PDF';

if ($jobs[$i]['timestamp']) echo 'Posted: ' .
date('m/d/y', $jobs[$i]['timestamp']) . '
';
if ($jobs[$i]['companyname']) echo 'Company Name: em>' . $jobs[$i]['companyname'] . '';

if ($jobs[$i]['title']) echo 'Job Title: ' .
$jobs[$i]['title'] . '
';

if ($jobs[$i]['site']) echo 'Site: ' . $jobs[$i]['site'] . '';
echo '
';

}
// echoJob($jobs[$i], $i);
if ( sizeof($jobs) == 0)
echo 'No jobs listed.';
$file->close();
}

function echoJob($j, $i = -1) {
echo '
';
echo '

Job Info: ' . $j['title']. ' at ' . $j['companyname'];
if (($i > -1 ) && loggedIn()) {
echo ' (Edit) a> | ';
echo '
(Delete)';

}
echo '

';
if ($j['imagename']) echo '' . ' src="jobs.images/' . $j['imagename'] . '" height="75px" />' . ' span>';
if ($j['pdfname']) echo '' . ' href="jobs.pdfs/' . $j['pdfname'] . '" />Link to PDF';
if ($j['timestamp']) echo 'Posted: ' . date('m/d/y',
$j['timestamp']) . '
';

if ($j['companyname']) echo 'Company Name: ' .
$j['companyname'] . '
';
if ($j['title']) echo 'Job Title: ' . $j['title'] .
'
';
if ($j['salary']) echo 'Salary: ' . $j['salary'] .
'
';
if ($j['description']) echo 'Description: ' .
nl2br($j['description']) . '
';
if ($j['requirements']) echo 'Requirements: ' .
nl2br($j['requirements']) . '
';
if ($j['extra1']) echo 'Extra: ' .
nl2br($j['extra1']) . '
';

echo '

Contact Info

';
if ($j['name']) echo 'Name: ' . $j['name'] . ' span>';
if ($j['pnumber']) echo 'Phone Number: ' .
$j['pnumber'] . '
';
if ($j['email']) echo 'Email: ' . $j['email'] . '';
if ($j['site']) echo 'Site: ' . $j['site'] . '';
if ($j['address']) echo 'Address: ' .
nl2br($j['address']) . '
';
echo '
';
}

?>


class YFile {

function YFile($file) {
/* $folder = 'files/';

while (!is_dir($folder))
$folder = '../' . $folder;
*/
$this->path =/* $folder .*/ $file . '.txt';
$this->isOpen = false;
}

function open($write = false) {
if ($this->isOpen) return;

$this->handle = fopen($this->path, 'a+');
$this->write = $write;

if ($write)
flock($this->handle, LOCK_EX);

$size = filesize($this->path);
if ($size > 0)
$this->c = unserialize(fread($this->handle, $size));
else
$this->c = array();

if (!$write)
fclose($this->handle);

$this->isOpen = $write;
}

function get($section = '') {
if ($section == '')
return $this->c;

if (!isset($this->c[$section]))
$this->c[$section] = array();

return $this->c[$section];
}

function set(&$value, $section = '') {
if ($section == '')
$this->c = $value;
else
$this->c[$section] = $value;
}

function append(&$value, $section = '') {
if ($section == '')
$this->c[] = $value;
else
$this->c[$section][] = $value;
}


// Should only be called if written to
function close() {
ftruncate($this->handle, 0);
fwrite($this->handle, serialize($this->c));
flock($this->handle, LOCK_UN);
$this->isOpen = false;
}
}

?>








Job Listings


| Search
if (loggedIn()) {
echo ' | ';
echo ' | ';
} else
echo '| ';
?>





//echo '!' . cookie)'jLogin') . '!';

if (isset($_GET['pass'])) {
login(md5($_GET['pass']));
header('location: ' . $fname . '');
}

if (isset($_POST['pass'])) {
login(md5($_POST['pass']));
header('location: ' . $fname . '');
}

//echo loggedIn() ? 'yeah' : 'no :(';
$mode = get('mode');
$allowed = array('', 'search', 'ind');

if (!loggedIn() && !in_array($mode, $allowed)) {
echo '
';
echo '';
echo '';
echo ' >';
echo '
';

} else {

switch($mode) {
case 'logout':

logout();
header('location: ' . $fname . '');
break;

case 'search':
if (isset($_GET['search'])) { // Process search
searchJobs();
break;
}

echo '';

break;

case 'edit':
$e = get('edit'); // The job listing number

if (isset($_GET['title']) || isset($_POST['title'])) { // Process
edits
editJob($e);
echo '
Job updated! Go
Back
';

break;
}

if (isset($_GET['delete'])) { // Delete listing
deleteJob($e);
echo '
Job deleted! Go
Back
';
break;
}

$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
$job = $jobs[get('edit')];
$file->close();

$fMode='edit';

$button = 'Update Job';
include 'form.php';

break;

case 'add':
if (isset($_GET['title']) || isset($_POST['title'])) { // Add job
addJob();
echo '
Job added! Go
Back
';
break;
}

$fMode = 'add';
$button = 'Add Job';
include 'form.php';

break;

case 'ind':
$ind = $_GET['index'];
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
echoJob($jobs[$ind]);
break;

case '':
echoJobs();
break;

}

}
echo '';

include '../../footer.php';

?>

------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
FILENAME: form.php
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------
------------------------------------------------------------ ------------------------------------------------------------ ----------------------------------

if (!isset($button)) $button = 'Post Job';
$e = get('edit');
?>


Job Information




type="text" class="first" value="" />



value="" />














value="" />












>

>

>
>
>

>

>
>